import react from '@vitejs/plugin-react'
import { resolve } from 'path'
import { UserConfig } from 'vite'
import copy from 'rollup-plugin-copy'

const { npm_package_name, npm_package_version } = process.env

function pathResolve(dir: string) {
  return resolve(__dirname, '.', dir)
}

const config: UserConfig = {
  build: {
    target: 'esnext'
  },
  resolve: {
    alias: [
      {
        find: /@\//,
        replacement: pathResolve('src') + '/'
      }
    ]
  },

  plugins: [
    react(),
    copy({
      targets: [
        { src: './manifest.json', dest: 'dist' },
        {
          src: ['dist/*', '!**/*.zip', './manifest.json'],
          dest: `../../src-tauri/extensions/${npm_package_name}`
        }
      ],
      verbose: true,
      hook: 'writeBundle'
    })
  ]
}

const getConfig = () => config

export default getConfig
